import 'package:flutter/material.dart';
import 'package:goumao_app/Customs/gm_app_util.dart';

import '../Manager/gm_user_manager.dart';
import 'gm_chat_ui.dart';
// import 'package:xin_heart_app/model/home_model.dart';
// import 'package:xin_heart_app/utils/app_utils.dart';
// import 'package:xin_heart_app/utils/xy_user.dart';

class GMChatListPage extends StatefulWidget {
  const GMChatListPage({super.key});

  @override
  State<GMChatListPage> createState() => _ChatPageState();
}

class _ChatPageState extends State<GMChatListPage> {
  // final List<String> localItems = [
  //   "系统通知=欢迎来到=assets/images/gm_xx_tz_icon@2x.png",
  //   "官方客服=你好，有什么可以帮助你的吗=assets/images/gm_gf_kf_icon@2x.png"
  // ];

  // Future<List<HomeData>> loadUserListFromLocal() async {
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   String? jsonString = prefs.getString('userList');

  //   if (jsonString != null && jsonString.isNotEmpty) {
  //     List<dynamic> userMapList = json.decode(jsonString);
  //     return userMapList.map((userMap) => HomeData.fromJson(userMap)).toList();
  //   }

  //   return [];
  // }

  // Future<void> loadUserList() async {
  //   List<HomeData> loadedUserList = await loadUserListFromLocal();
  //   setState(() {
  //     userList = loadedUserList;
  //   });
  // }

  @override
  void initState() {
    super.initState();

    // 在页面初始化时加载本地用户数据列表
    // loadUserList();
  }

  @override
  Widget build(BuildContext context) {
    // Widget chatListCell(
    //     int index, String title, String des, String url, String time) {
    //   bool isNetUrl = url.contains("http");
    //   return
    // }

    return Scaffold(
      appBar: AppBar(
        title: GMAppUtil.navText("我的消息"),
        elevation: 0,
      ),
      body: ListView.builder(
          itemCount: 2,
          itemBuilder: (context, index) {
            // List<String> datas = localItems[index].split("=");
            if (index == 0) {
              return const ChatListView(
                index: 0,
                title: "系统通知",
                des: "欢迎来到美愿",
                url: 'assets/images/gm_xx_tz_icon@2x.png',
                time: "",
              );
            } else if (index == 1) {
              return const ChatListView(
                index: 1,
                title: "官方客服",
                des: "你好，有什么可以帮助你的吗",
                url: 'assets/images/gm_gf_kf_icon@2x.png',
                time: "",
              );
            }
            return null;
            //  else {
            //   return Container();
            //   // HomeData data = userList[index - localItems.length];
            //   // return ChatListView(index: index, title: data.userCardBaseInfo!.userName!,des: data.wishId!,url: data.userCardBaseInfo!.userIcon!,time: data.makeTime!,data: data,);
            // }
          }),
    );
  }
}

class ChatListView extends StatefulWidget {
  final String title;
  final String des;
  final String time;
  final String url;
  final int index;

  const ChatListView(
      {super.key,
      required this.title,
      required this.des,
      required this.time,
      required this.url,
      required this.index});

  @override
  State<ChatListView> createState() => _ChatListViewState();
}

class _ChatListViewState extends State<ChatListView> {
  @override
  Widget build(BuildContext context) {
    bool isNetUrl = widget.url.contains("http");
    return Container(
        padding: const EdgeInsets.fromLTRB(12, 16, 12, 8),
        height: 90,
        child: InkWell(
          onTap: () {
            if (GMUserManager().isLoggedIn) {
              if (widget.index == 0) {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => GMChatPage(
                              title: "系统通知",
                            )));
              } else if (widget.index == 1) {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => GMChatPage(
                              title: "官方客服",
                            )));
              } else {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => GMChatPage(
                              title: widget.title,
                            )));
              }
            } else {
              // Constants.navigateToLogin(context);
            }
          },
          
          child: SizedBox(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                !isNetUrl
                    ? Image.asset(
                        widget.url,
                        width: 66,
                        height: 66,
                      )
                    : ClipRRect(
                        borderRadius: BorderRadius.circular(12),
                        child: Image.network(
                          widget.url,
                          width: 66,
                          height: 66,
                        ),
                      ),
                const SizedBox(
                  width: 8,
                ),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Row(
                        children: [
                          Text(
                            widget.title,
                            style: const TextStyle(
                                color: Color(0xff1a1a1a),
                                fontSize: 14,
                                fontWeight: FontWeight.w700),
                          ),
                          const SizedBox(
                            width: 4,
                          ),
                        ],
                      ),
                      const SizedBox(
                        height: 8,
                      ),
                      Text(
                        widget.des,
                        style: const TextStyle(
                            color: Color(0xFF999999), fontSize: 12),
                      )
                    ],
                  ),
                ),
                Expanded(
                    flex: 0,
                    child: Text(
                      widget.time,
                      style: const TextStyle(
                          color: Color(0xFFCCCCCC), fontSize: 12),
                    ))
              ],
            ),
          ),
        ));
  }
}


// class MessageScreen extends StatefulWidget {
//   const MessageScreen({super.key});

//   @override
//   // ignore: library_private_types_in_public_api
//   _MessageScreenState createState() => _MessageScreenState();
// }

// class _MessageScreenState extends State<MessageScreen> {
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(
//         elevation: 0,
//         title: GMAppUtil.navText("title"),
//       ),
//       body: Center(
//         child: Text('这是消息页面'),
//       ),
//     );
//   }
// }

// import 'package:flutter/material.dart';

// class GMCWDananScreen extends StatefulWidget {
//   const GMCWDananScreen({super.key});

//   @override
//   // ignore: library_private_types_in_public_api
//   _GMCWDananScreenState createState() => _GMCWDananScreenState();
// }

// class _GMCWDananScreenState extends State<GMCWDananScreen>  {
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       backgroundColor: const Color(0xffF8F8F8),
//       appBar: AppBar(
//         elevation: 0,
//         title:const Text('宠物档案', style: TextStyle(color: Color(0xff333333), fontSize: 16, fontWeight: FontWeight.w800),),
//       ),
//       body: SafeArea(
//         child: Column(
//           children: [
//             // 可滑动的页面
//             Expanded(
//               child: SingleChildScrollView(
//                 child: Column(
//                   children: [
//                     Container(height: 1000,color: Colors.orange,)
//                   ],
//                 ),
//               ),
//             ),
//             // 底部固定高度的 Container
//             Container(
//               height: 74.0,
//               padding: const EdgeInsets.fromLTRB(33, 0, 33, 20),
//               child: InkWell(
//                 child: Container(
//                   decoration: BoxDecoration(
//                     borderRadius: BorderRadius.circular(25),
//                     color: const Color(0xffFF6A6E)
//                   ),
//                   child: const Center(
//                     child: Text("保存", style: TextStyle(color: Color(0xffffffff), fontSize: 15, fontWeight: FontWeight.w400),),
//                   ),
//                 ),
//               ),
//             ),
//           ],
//         ),
//       ),
//     );
//   }
// }
